import { defineStore } from 'pinia';
import { getCommentList, reviewComment, deleteComment, replyComment } from '@/api/comment';

export const useCommentStore = defineStore('comment', {
  state: () => ({
    commentList: [],
    total: 0,
    loading: false
  }),
  
  actions: {
    async getCommentList(pageNum = 1, pageSize = 10, status?: number) {
      this.loading = true;
      try {
        const res: any = await getCommentList({ pageNum, pageSize, status });
        this.commentList = res.records;
        this.total = res.total;
        return res;
      } catch (error) {
        return Promise.reject(error);
      } finally {
        this.loading = false;
      }
    },
    
    async reviewComment(commentId: number, status: number) {
      try {
        await reviewComment(commentId, status);
        return true;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    
    async deleteComment(commentId: number) {
      try {
        await deleteComment(commentId);
        // 删除成功后更新列表
        this.commentList = this.commentList.filter((comment: any) => comment.id !== commentId);
        return true;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    
    async replyComment(data: {
      articleId: number;
      content: string;
      parentId: number;
      replyToUserId: number;
    }) {
      try {
        return await replyComment(data);
      } catch (error) {
        return Promise.reject(error);
      }
    }
  }
}); 